package com.zhiqianbao.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.entity.User;
import com.zhiqianbao.entity.UserCheck;
import com.zhiqianbao.entity.UserInfo;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by liang on 2015/12/29.
 */
public class IdCardMessageActivity extends BaseActivity {
    /**
     * 姓名
     */
    TextView textViewUsername;
    /**
     * 身份证号
     */
    TextView textviewIdCardInfo;
    /**
     * 审核是否通过
     */
    TextView textViewIsOverLook;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_id_card_message);
        init();
        initView();
    }

    private void initView() {
         textViewUsername=(TextView)findViewById(R.id.textView_username);
         textviewIdCardInfo=(TextView)findViewById(R.id.textview_id_card_info);
        textViewIsOverLook=(TextView)findViewById(R.id.textView_isOverLook);
        String userId = Constants.LOGIN_USER.getId().toString();
        List<UserInfo> userInfos = UserInfo.findWithQuery(UserInfo.class, "select * from user_info where user_id = ? ", userId);
        UserInfo userInfo = userInfos.get(0);
        textViewUsername.setText(userInfo.getName());
        textviewIdCardInfo.setText(userInfo.getIdcard());
        //已通过审核
        //textViewIsOverLook.setText("已通过");
        Map<String, String> param = new HashMap<>();
        param.put("token", Constants.LOGIN_USER.getToken());
        param.put("userId", Constants.LOGIN_USER.getId().toString());
        new UserCheckAsyncTask().execute(param);

    }

    private class UserCheckAsyncTask extends AsyncTask<Map, Void, Result<UserCheck>> {

        @Override
        protected void onPreExecute() {
            showWaitDialog();
        }

        @Override
        protected Result<UserCheck> doInBackground(Map... params) {
            String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/userCheck", params[0], IdCardMessageActivity.this);
            Result<UserCheck> resultObject = Result.fromJson(result, UserCheck.class);
            return resultObject;
        }

        @Override
        protected void onPostExecute(Result<UserCheck> result) {

            if (result!=null&&(!TextUtils.isEmpty(result.getCode()))) {
                if (Constants.DEFAULT_SUCCESS_CODE.equals(result.getCode())) {
                   // showShortToast1(result.getMsg());
                    UserCheck userCheck = result.getData();
                    //审核通过
                    if (Constants.USER_AUDIT_STATE_SUCCESS.equals(userCheck.getCheckState())) {
                        textViewIsOverLook.setText("已通过");
                        //将用户状态改为已通过审核
                        Constants.LOGIN_USER.setInfoState(Constants.USER_STATE_AUDIT_INFO);
                        //更新数据库中user状态
                        User.executeQuery("update user set info_state = ? where id = ?", Constants.USER_STATE_AUDIT_INFO, Constants.LOGIN_USER.getId().toString());
                    } else if (Constants.USER_AUDIT_STATE_FAILED.equals(userCheck.getCheckState())) {
                        textViewIsOverLook.setText("未通过");
                    } else {
                        textViewIsOverLook.setText("未审核");
                    }
                } else {
                    textViewIsOverLook.setText("未审核");
                    showShortToast1(result.getMsg());
                }
                closeWaitDialog();
            }
        }
    }
}